package com.qf.dao.impl;


import com.mysql.jdbc.Connection;
import com.qf.dao.TransferDao;
import com.qf.model.UserCard;
import com.qf.utils.DateUtil;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.math.BigDecimal;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TransferDaoImpl implements TransferDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    //提现
    public int withdrawal(String cid, BigDecimal amount) {

        UserCard Account = getAccount(cid);//获得卡信息
        BigDecimal money = Account.getMoney();//获得卡余额

        int m = money.compareTo(amount);//比较余额和转账金额

        if (Account != null) {
            if (m != -1) {

                String sql1 = "update usercard set money=money-? where cid=?";
                try {
                    int tx = queryRunner.update(sql1, amount, cid);
                    return tx;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    //充值
    @Override
    public int recharge(String cid, BigDecimal amount) {
        String sql = "update usercard set money=money+? where cid=?";
        try {
            int cz = queryRunner.update(sql, amount, cid);
            return cz;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //入账
    @Override
    public void inMoney(String toAccountId, BigDecimal amount) {
        String sql = "update usercard set money = money + ? where cid = ?";
        try {
            int update = queryRunner.update(sql, amount, toAccountId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //出账
    @Override
    public void outMoney(String fromAccountId, BigDecimal amount) {
        String sql = "update usercard set money = money - ? where cid = ?";
        try {
            int update = queryRunner.update(sql, amount, fromAccountId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //根据用卡id获取卡信息
    @Override
    public UserCard getAccount(String cid) {
        String sql = "select * from usercard  where cid=?";
        try {
            UserCard usercard = queryRunner.query(sql, new BeanHandler<>(UserCard.class), cid);
            return usercard;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
//DruidUtils druidUtils=new DruidUtils();
//
//    public int transfer(String fromAccountId, String toAccountId, BigDecimal amount) throws Exception {
//        Connection conn = null;
//        PreparedStatement ps = null;
//        ResultSet result = null;
//        try{
//
//
//            //扣除账户钱
//            String sql = "update usercard set balance = balance - ? where name = ?";
//            ps = conn.prepareStatement(sql);
//            ps.setBigDecimal(1, amount);
//            ps.setString(2, fromAccountId);
//            ps.executeUpdate();
//
//
//            //账户增加钱
//            sql = "update usercard set balance = balance + ? where name = ?";
//            ps = conn.prepareStatement(sql);
//            ps.setBigDecimal(1, amount);
//            ps.setString(2, toAccountId);
//            ps.executeUpdate();
//
//            conn.commit();//提交（要整个事务都完成才能提交，因为提交之后就不能改变了）
//        }catch(Exception e){
//            e.printStackTrace();
//            conn.rollback();
//        }
//        druidUtils.close(conn, ps, result);
//        return 1;
//    }


}